package terms;

import java.util.Collection;

import substitution.HashUnifier;

/**
 * Mar 29, 2012
 * Ecole des Mines de Nantes
 * Major in Information and Communications Systems Engineering.
 * @author Pierre Reliquet, Simon Devineau
 */
public class Variable extends AbstractTerm {
    /**
     * The char representation of a variable.
     */
    private String name;

    /**
     * Constructor.
     * @param var
     *            String, the name of the variable.
     */
    public Variable(final String var) {
        super();
        this.name = var;
    }

    /**
     * @param anObject
     *            the object to be equal.
     * @return true if anObject is a variable equals to this.
     */
    @Override
    public final boolean equals(final Object anObject) {
        boolean result = false;
        if (HashUnifier.isVariable(anObject)) {
            result = ((Variable) anObject).getName().equals(this.getName());
        }
        return result;
    }

    /**
     * @return 1
     */
    @Override
    public int getLength() {
        return 1;
    }

    /**
     * @return the name
     */
    @Override
    public String getName() {
        return this.name;
    }

    /**
     * @return null
     */
    @Override
    public Collection<AbstractTerm> getRest() {
        return null;
    }

    /**
     * @return the hashcode of the variable
     */
    @Override
    public final int hashCode() {
        return this.getName().hashCode();
    }

    /**
     * @return false
     */
    @Override
    public boolean isRest() {
        return false;
    }

    /**
     * @param aName
     *            the name to set
     */
    public void setName(final String aName) {
        this.name = aName;
    }

    /**
     * @return a string representation of the variable.
     */
    @Override
    public final String toString() {
        return this.getName();
    }
}
